package com.ewzj.bigdata.mvp.ui.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.extensions.loadImgwithloading
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.di.component.DaggerMineFragmentComponent
import com.ewzj.bigdata.di.module.MineFragmentModule
import com.ewzj.bigdata.mvp.contract.MineFragmentContract
import com.ewzj.bigdata.mvp.model.bean.UserBean
import com.ewzj.bigdata.mvp.presenter.MineFragmentPresenter
import com.ewzj.bigdata.mvp.ui.activity.*
import com.gyf.barlibrary.ImmersionBar
import com.jakewharton.rxbinding2.view.RxView
import com.jess.arms.base.App
import com.jess.arms.base.BaseApplication
import com.jess.arms.base.BaseFragment
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.UiUtils
import com.jess.arms.widget.imageloader.glide.GlideImageConfig
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_mine.*
import me.iwf.photopicker.PhotoPicker

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class MineFragment : BaseFragment<MineFragmentPresenter>(), MineFragmentContract.View {
    val imageLoader by lazy { (this.activity.applicationContext as BaseApplication).appComponent.imageLoader() }
    lateinit var userinfo :UserBean
    override fun setBgImg(bgimg: String) {
        iv_head_bg.loadImgwithloading(bgimg)
    }

    override fun loadheadImg(img: String) {
        Glide.with(activity).load(img).asBitmap().dontTransform().dontAnimate().error(R.drawable.ic_loading)
                .placeholder(R.drawable.ic_loading).into(headimage)
//        headimage.loadImgwithloading(img)
    }

    override fun setNickName(user: UserBean) {
        tv_name.text = user.nickname
        userinfo=user
    }

    companion object {
        fun newInstance(): MineFragment {
            val fragment = MineFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    override fun setupFragmentComponent(appComponent: AppComponent?) {
        DaggerMineFragmentComponent.builder().appComponent(appComponent)
                .mineFragmentModule(MineFragmentModule(this)).build()
                .inject(this);
    }

    override fun setData(data: Any?) {

    }

    override fun initData(savedInstanceState: Bundle?) {
        initListener()
    }

    override fun onResume() {
        super.onResume()
        if (Apputils.Utils.checkIsLogin(activity)) {
            mPresenter.loadUserInfo()
        } else {
            Glide.with(activity).load(R.drawable.ic_loading).dontTransform().dontAnimate().into(headimage)
            Glide.with(activity).load(R.drawable.ming_bg).into(iv_head_bg)
            tv_name.text = getString(R.string.default_name)
        }
    }

    override fun initView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val view = inflater!!.inflate(R.layout.fragment_mine, null)
        return view
    }

    fun initListener(): Unit {
        initIntentClient(Intent(activity, LoginActivity::class.java), tv_recentscan)
        RxView.clicks(tv_binding).subscribe({
            if (Apputils.Utils.checkIsLogin(activity)) {
                startActivity(Intent(activity, PhoneBindingActivity::class.java))
            } else {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        })
        RxView.clicks(iv_head_bg).subscribe({
            if (Apputils.Utils.checkIsLogin(activity)) {
                PhotoPicker.builder()
                        .setPhotoCount(1)
                        .setShowCamera(true)
                        .setShowGif(false)
                        .setPreviewEnabled(false)
                        .start(activity,this, PhotoPicker.REQUEST_CODE)
            } else {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        })
        RxView.clicks(llhead).subscribe({
            if (Apputils.Utils.checkIsLogin(activity)) {
                if (userinfo!=null){
                    EditProfileActivity.startEditProfileActivity(activity,userinfo)
                }
            } else {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        })
        RxView.clicks(tv_collection).subscribe({
            if (Apputils.Utils.checkIsLogin(activity)) {
                startActivity(Intent(activity, CollectActivity::class.java))
            } else {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        })
        RxView.clicks(tv_my_msg).subscribe({
            if (Apputils.Utils.checkIsLogin(activity)) {
                startActivity(Intent(activity, MessageActivity::class.java))
            } else {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        })
        RxView.clicks(tv_about).subscribe({
            startActivity(Intent(activity, AboutActivity::class.java))
        })
        RxView.clicks(tv_setting).subscribe({
            startActivity(Intent(activity, SettingActivity::class.java))
        })
    }

    fun initIntentClient(intent: Intent?, view: View): Unit {
        RxView.clicks(view).subscribe(Consumer {
            if (Apputils.Utils.checkIsLogin(activity)) {
                startActivity(Intent(activity, ScanRecordActivity::class.java))
            } else {
                startActivity(Intent(activity, LoginActivity::class.java))
            }
        })


    }

    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {
        startActivity(intent)
    }

    override fun hideLoading() {

    }

    override fun killMyself() {

    }

    override fun showMessage(message: String?) {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode!= Activity.RESULT_CANCELED&&data!=null){
            when(requestCode){
                PhotoPicker.REQUEST_CODE->{
                    if (data != null) {
                        val photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS)
                        if (photos!=null&&photos.size>0){
//                            iv_head_bg.loadImgwithloading(photos[0])
                            mPresenter.uploadPic(photos[0])
                        }

                    }
                }
            }
        }
    }

    override fun updateUserBg(bgimg: String) {
        iv_head_bg.loadImgwithloading(Apputils.Utils.getImgUrl(bgimg))
    }

    fun setStatusBar(mImmersionBar:ImmersionBar){
        mImmersionBar.reset().statusBarDarkFont(false, 0.2f).init()
    }
}